package com.ld.xuanKe.service.impl;

import com.ld.xuanKe.dao.XsTeachingClassDAO;
import com.ld.xuanKe.entity.*;
import com.ld.xuanKe.service.SelectdService;
import com.ld.xuanKe.service.TeachingClassService;
import com.ld.xuanKe.service.UserService;
import com.ld.xuanKe.service.XsTeachingClassService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName xuanKe
 * @ClassName XsTeachingClassServiceImpl
 * @Author 刘邓
 * @Date 2023-12-20 21:50
 */
@Service
public class XsTeachingClassServiceImpl implements XsTeachingClassService {
	@Resource
	private XsTeachingClassDAO xsTeachingClassDAO;
	@Resource
	private SelectdService selectdService;
	@Resource
	private TeachingClassService teachingClassService;
	@Resource
	private UserService userService;

	public int add(XsTeachingClass xsTeachingClass) {
		int insert;
		XsTeachingClass entity = xsTeachingClassDAO.findOne("from XsTeachingClass T where T.teachingclassId = ? and T.userId = ?", new Object[]{xsTeachingClass.getTeachingclassId(),xsTeachingClass.getUserId()});
		if(entity == null){
			insert = xsTeachingClassDAO.insert(xsTeachingClass);
		}else {
			insert = 0;
		}
		return insert;
	}

	public int addList() {
		List<Selectd> selectds = selectdService.getValidSelectListAfterSelect();
		int insert = 0;
		for (Selectd selectd : selectds) {
			XsTeachingClass xsTeachingClass = new XsTeachingClass();
			xsTeachingClass.setUserId(selectd.getUserId());
			TeachingClass teachingClass = teachingClassService.querySingleByCourseId(selectd.getCourseId());
			xsTeachingClass.setTeachingclassId(teachingClass.getId());
			insert += this.add(xsTeachingClass);
		}
		return insert;
	}

	public List<XsTeachingClass> list() {
		return xsTeachingClassDAO.find("from XsTeachingClass",new Object[]{});
	}

	public List<XsTeachingClassDisplay> displayListByClassId(Integer classId) {
		List<XsTeachingClass> list = xsTeachingClassDAO.find("from XsTeachingClass X where X.teachingclassId = ?", new Object[]{classId});
		List<XsTeachingClassDisplay> list1 = new ArrayList<XsTeachingClassDisplay>();
		XsTeachingClassDisplay xsTeachingClassDisplay = new XsTeachingClassDisplay();
		User user = new User();
		for (XsTeachingClass xsTeachingClass : list) {
			user = userService.querySingle(xsTeachingClass.getUserId());
			xsTeachingClassDisplay.setTeachingClassId(xsTeachingClass.getId());
			xsTeachingClassDisplay.setAccount(user.getAccount());
			xsTeachingClass.setUserId(user.getId());
			xsTeachingClassDisplay.setUsername(user.getUsername());
			list1.add(xsTeachingClassDisplay);
		}
		return list1;
	}


	public List<XsTeachingClassDisplay> displayList() {
		List<XsTeachingClass> list = list();
		List<XsTeachingClassDisplay> list1 = new ArrayList<XsTeachingClassDisplay>();
		XsTeachingClassDisplay xsTeachingClassDisplay = new XsTeachingClassDisplay();
		User user = new User();
		for (XsTeachingClass xsTeachingClass : list) {
			user = userService.querySingle(xsTeachingClass.getUserId());
			xsTeachingClassDisplay.setTeachingClassId(xsTeachingClass.getId());
			xsTeachingClassDisplay.setAccount(user.getAccount());
			xsTeachingClassDisplay.setUserId(user.getId());
			xsTeachingClassDisplay.setUsername(user.getUsername());
			list1.add(xsTeachingClassDisplay);
		}
		return list1;
	}


}
